const gulp = require("gulp");
const gulpLess = require("gulp-less");
const gulpSass = require("gulp-sass")(require("node-sass"));
const fs = require("fs");
const { Transform } = require("stream");

// //低版本写法
// gulp.task('test-sass',function(done){
//   gulp.src('src/sass/*.sass').pipe(gulpSass()).pipe(gulp.dest('dist/css/'));
//   done()
// })

// gulp.task('test-less',function(done){
//   gulp.src('src/less/*.less').pipe(gulpLess()).pipe(gulp.dest('dist/css/'));
//   done()
// })

// gulp.task('test-watch',function(done){
//   gulp.watch('src/less/*.less',['test-less']);
//   done()

// })

//4版本写法
const test_less = (done) => {
  gulp.src("src/sass/*.sass").pipe(gulpSass()).pipe(gulp.dest("dist/css/"));
  done();
};

// const stream = (done) => {
//   const readStream = fs.createReadStream("package.json");
//   const writeStream = fs.createWriteStream("temp.txt");
//   readStream.pipe(writeStream);
//   readStream.on("end", () => {
//     done();
//   });
// };

const stream = (done) => {
  const readStream = fs.createReadStream("package.json");
  const writeStream = fs.createWriteStream("temp.txt");
  readStream.pipe(writeStream);
  return readStream;
};

const streamT = (done) => {
  const readStream = fs.createReadStream("package.json");
  const writeStream = fs.createWriteStream("temp.txt");
  const transform = new Transform({
    transform: (chunk, encoding, callback) => {
      const input = chunk.toString();
      const output = input.replace(/\s+/g, "").replace(/\/\*.+?\*\//g, "");
      callback(null, output);
    },
  });

  readStream.pipe(transform).pipe(writeStream);
  return readStream;
};

module.exports = {
  test_less,
  stream,
  streamT,
};
